<?php
/**
 * 用户管理类
 * 
 * @author  Lee<a605333742@gmail.com>
 * @time    2016-07-25
 */
namespace Admin\Controller;

class UserController extends CommonController{
    /*
     * 构造函数
     * 
     * @return #
     */
    public function _initialize(){       
        parent::_initialize();
        $this->db   =new \Model\UserModel();
    }
    
    /*
     * 正常企业用户列表
     * 
     * @return  #
     */
    public function index(){
        if(I("request.nickName")){
            $map['nickName']    =I("request.nickName","",'trim');
        }
        if(I("request.companyName")){
            $map['companyName'] =I("request.companyName",'',"trim");
        }
        $map=$this->_search("User");
        //if(I("request.add_time")){
        //    $map['add_time']    =array("egt",I("request.add_time"));
        //}      
        $map['type']    =1;
        $map['state']   =2;
        $sortBy         ='add_time';
        $sort           =desc;
        $userList       =$this->_list($this->db, $map,$sortBy,$sort);
        $this->assign("userList",$userList);
        $this->assign("act",1);
        $this->display();
    }
    
    /*
     * 待审核企业用户
     * 
     * @return #
     */
    public function auditUser(){
        if(I("request.nickName")){
            $map['nickName']    =I("request.nickName","",'trim');
        }
        if(I("request.companyName")){
            $map['companyName'] =I("request.companyName",'',"trim");
        }
        $map=$this->_search("User");
        //if(I("request.add_time")){
        //    $map['add_time']    =array("egt",I("request.add_time"));
        //}  
        $map['type']    =1;
        $map['state']   =1;
        $sortBy         ='add_time';
        $sort           =desc;
        $userList       =$this->_list($this->db, $map,$sortBy,$sort);
        $this->assign("userList",$userList);
        $this->assign("act",2);
        $this->display("index");
    }
    
    /*
     * 个人用户列表
     * 
     * @return  #
     */
    
    public function inUser(){
        if(I("request.nickName")){
            $map['nickName']    =I("request.nickName","",'trim');
        }
        $map=$this->_search("User");
        if(I("request.add_time")){
            $map['add_time']    =array("egt",I("request.add_time"));
        }  
        $map['type']    =0;
        $map['state']   =2;
        //区别标志
        $sortBy         ='add_time';
        $sort           =desc;
        $userList       =$this->_list($this->db, $map,$sortBy,$sort);
        $this->assign("userList",$userList);
        $this->assign("act",3);
        $this->display("index");
    }
    
    /*
     * 已禁用的用户列表，包括个人和企业
     * 
     * @return #
     */
    public function noUser(){
        if(I("request.nickName")){
            $map['nickName']    =I("request.nickName","",'trim');
        }
        if(I("request.companyName")){
            $map['companyName'] =I("request.companyName",'',"trim");
        }
        $map=$this->_search("User");
        if(I("request.add_time")){
            $map['add_time']    =array("egt",I("request.add_time"));
        }  
        $map['state']   =3;
        $sortBy         ='add_time';
        $sort           =desc;
        $userList       =$this->_list($this->db, $map,$sortBy,$sort);
        $this->assign("userList",$userList);
        $this->assign("act",4);
        $this->display("index");
    }
    
    /*
     * 显示编辑用户
     * 
     * @return #
     */
    public function show(){
        //$region =D("Region")->select();
        //$this->assign("region",$region);
        //获取省份
        $provinceData = M('Area')->where("id like '__0000' and state='CN'")->select();
        $this->assign ('provinceData', $provinceData);
        if(I("request.id","","int")){
            $userInfo   =$this->db->find(I("request.id","","int"));            
            //获取地区
            if (!empty($userInfo['regionid'])){
                $areaInfo = M('Area')->where(array("id"=>$userInfo['regionid']))->find();
            }
            if ($areaInfo){
                //获取城市
                $code = substr($areaInfo['id'], 0, 2);
                $cityData = M('Area')->where("id like '{$code}__00' and state='CN' and id<>'{$code}0000'")->select();
                $this->assign ('cityData', $cityData);
                //获取地区
                $code = substr($areaInfo['id'], 0, 4);
                $areaData = M('Area')->where("id like '{$code}__' and state='CN' and id<>'{$code}00'")->select();
                $this->assign ('areaData', $areaData);
                $this->assign('areaInfo', $areaInfo);
            }
            $this->assign("userInfo",$userInfo);
        }
        if(I("request.state")){
            $this->assign("state",1);
        }
        if(I("request.sign")){
            $this->assign('sign',I("request.sign"));
        }
        $this->display();       
    } 
    
    /*
     * 编辑用户
     * 
     * @return #
     */
    public function edit(){
        if(!empty($_POST)){   
            if(I("post.id")){
                $map['id']        =I("post.id"); 
            }
            if(I("post.nickName")){
                $map['nickName']=I("post.nickName");
            }else{
                $this->error("昵称不能为空");
            }
            if(I("post.password")){
                $map['password']=I("post.password","","md5");
            }else if(!I("post.password") && !I("post.id")){
                $this->error("密码不能为空");
            }
            if(I("post.tel")){
                $map['tel']     =I("post.tel");
            }
            if(I("post.sex")){
                $map['sex']     =I("post.sex");
            }
            if(I("post.type")){
                $map['type']    =I("post.type");
                if(I("post.type")==1){
                    $map['state']=1;
                }
            }else{
                $map['type']        =0;
            }
            if(I("post.companyName")){
                $map['companyName'] =I("post.companyName","","trim");
            }
            
            if(I("request.province")){
                $map['regionId']    =I("post.province","","int");
            }
            if(I("request.city")){
                $map['regionId']    =I("post.city","","int");
            }
            if(I("post.integral")){
                $map['integral']    =I("post.integral",'',"int");
            }
            if(I("request.websit")){
                $map['websit']      =I("request.websit");
            }
            if(I("request.addroduction")){
                $map['addroduction']=I("request.addroduction");
            }
            if(I("request.icon")){
                $map['icon']        =I("request.icon");
            }
            if(I("request.icon")){
                $map['icon']        =I("request.icon");
            }
            if(I("request.license")){
                $map['license']     =I("request.license");
            }
            $userEdit       =$this->db->edit($map);
            unlink('Application/Public/'.$_SESSION['icon']);            
            if($userEdit){ 
                $this->success('添加成功');              
            }else{
                $this->error("编辑失败");
            }
        }else{
            $this->error("编辑失败");
        }
    }
    
    /*
     * 删除指定用户，批量删除用户
     * 
     * @return bool
     */
    public function del(){ 
        if(is_array(I("request.id"))){           
            $id =array("In",implode(",", I("request.id")));
        }else{            
            $id =I("request.id");
        }
        $map['id']      =$id;
        $map['state']   =4;
        if($this->db->edits($map)){            
            $this->success("删除成功",U("index"));
        }else{
            $this->error("删除失败",U("index"));
        }
    }
    
    /*
     * 禁用，批量禁用数据
     * 
     * @return #
     */
    public function noUsers(){
        if(is_array(I("request.id"))){
            $id =array("In"=>implode(",", I("request.id")));
        }else{
            $id =I("request.id");
        } 
        $map['id']      =$id;
        $map['state']   =3;
        if($this->db->edits($map)){
            $this->success("用户禁用成功",U("index"));
        }else{
            $this->error("禁用失败",U("index"));
        }
    }
    
    /*
     * 审核，批量审核，启用，批量启用
     * 
     * @return #
     */
    public function allEdit(){
        if(is_array(I("request.id"))){
            $map['id']  =array("In"=>implode(",", I("request.id")));
        }else{
            $map['id'] =I("request.id");
        }
        $map['state']   =2;       
        if($this->db->edits($map)){
            $this->success("用户审核成功",U("index"));
        }else{
            $this->error("用户审核失败",U("index"));
        }
    }
    
    /*
     * 待审核，批量待审
     * 
     * @return #
     */
    public function audUser(){
        if(is_array(I("request.id"))){
            $id =array("In"=>implode(",", I("request.id")));
        }else{
            $id =I("request.id");
        } 
        $map['id']      =$id;
        $map['state']   =1;
        if($this->db->edits($map)){
            $this->success("用户待审核设置成功",U("index"));
        }else{
            $this->error("用户待审核设置失败",U("index"));
        }
    }
    
    /*
     * 头像，LOGO上传处理
     * 
     * @return $url
     */
    public function upicon(){
        if(!empty($_FILES['icon']['name'])){            
            $config =array(
                'rootPath'  =>'./Application/Public/upload/',                              //根目录
                'savePath'  =>'userZZ/',                                            //保存路径
                'maxSize'   =>'902400',                                             //上传的文件大小限制 (0-不做限制)
                'exts'      =>array('jpg','png','gif','jpeg',"JPG","PNG","JPEG","GIF")    //允许上传的文件后缀
            );
            //附件被上传到路径：根目录/保存目录路径/创建日期目录
            $new_upload =new \Think\Upload($config);
            //uploadOne会返回已经上传的附件信息
            $upload     =$new_upload->uploadOne($_FILES['icon']);
            if(!$upload){
                echo $new_upload->getError();
            }else{
               echo $upload['savepath'].$upload['savename'];
            }           
        }
    }
}

